let { projectRootPath } = require("../../util/path/index.js");
let {
  executeSql,
  transaction,
  getTotal,
  getByPagination,
  removeByUuid,
  getAll,
} = require(`${projectRootPath}/mysql/index.js`);
let { getUuid } = require(`${projectRootPath}/util/uuid/index.js`);
let { getCurrentDatetime } = require(
  `${projectRootPath}/util/datetime/index.js`,
);

// 创建备忘
async function createNote(data) {
  let { userUuid, content, sendDatetime } = data;
  return await transaction(async (connection) => {
    let currentDatetime = getCurrentDatetime();
    let sql = `insert into NoteTable (uuid, userUuid, content, sendDatetime, isComplete, opDatetime) values ('${getUuid()}', '${userUuid}', '${content}', '${sendDatetime}', '2', '${currentDatetime}')`;
    await executeSql(connection, sql);
  });
}

// 获取备忘分页数据
async function getNoteByPagination(data) {
  let { pageSize, currentPage, userUuid, sendDatetimeRange, isComplete } = data;
  let where = "";
  let allWhere = [];
  if (userUuid) {
    where = `userUuid='${userUuid}'`;
    allWhere.push(where);
  }
  if (sendDatetimeRange.length === 1) {
    where = `sendDatetime between '${sendDatetimeRange[0]} 00:00:00' and '${sendDatetimeRange[0]} 23:59:59'`;
    allWhere.push(where);
  } else if (sendDatetimeRange.length === 2) {
    where = `sendDatetime between '${sendDatetimeRange[0]} 00:00:00' and '${sendDatetimeRange[1]} 23:59:59'`;
    allWhere.push(where);
  }
  if (isComplete !== "") {
    where = `isComplete='${isComplete}'`;
    allWhere.push(where);
  }
  if (allWhere.length > 0) {
    allWhere = "where " + allWhere.join(" and ");
  } else {
    allWhere = "";
  }
  return await transaction(async (connection) => {
    let list = await getByPagination(
      connection,
      "NoteTable",
      "sendDatetime",
      allWhere,
      pageSize,
      currentPage,
    );
    let total = await getTotal(connection, "NoteTable", "opDatetime", allWhere);
    return { list, total };
  });
}

// 获取备忘分页数据通过 uuid
async function getNoteByPaginationAndUuid(data) {
  let { pageSize, currentPage, uuid } = data;
  let where = "";
  let allWhere = [];
  if (uuid) {
    where = `uuid='${uuid}'`;
    allWhere.push(where);
  }
  if (allWhere.length > 0) {
    allWhere = "where " + allWhere.join(" and ");
  } else {
    allWhere = "";
  }
  return await transaction(async (connection) => {
    let list = await getByPagination(
      connection,
      "NoteTable",
      "sendDatetime",
      allWhere,
      pageSize,
      currentPage,
    );
    let total = await getTotal(connection, "NoteTable", "opDatetime", allWhere);
    return { list, total };
  });
}

// 获取未完成数量
async function getNoteNotCompleteCount(data) {
  let { userUuid } = data;
  return await transaction(async (connection) => {
    let sql = `select count(*) as count from NoteTable  where userUuid='${userUuid}' and isComplete='2' order by opDatetime`;
    let result = await executeSql(connection, sql);
    return { count: result[0].count };
  });
}

// 通过 uuid 编辑备忘
async function editNoteByUuid(data) {
  let { uuid, userUuid, content, isComplete, sendDatetime } = data;
  return await transaction(async (connection) => {
    let sql = `update NoteTable set userUuid='${userUuid}', content='${content}', isComplete='${isComplete}', sendDatetime='${sendDatetime}' 
                        where uuid='${uuid}'`;
    await executeSql(connection, sql);
  });
}

// 通过 uuid 删除备忘
async function removeNoteByUuid(data) {
  let { uuid } = data;
  return await transaction(async (connection) => {
    await removeByUuid(connection, "NoteTable", uuid);
  });
}

module.exports = {
  createNote,
  getNoteByPagination,
  getNoteNotCompleteCount,
  editNoteByUuid,
  removeNoteByUuid,
  getNoteByPaginationAndUuid,
};
